from django.shortcuts import render, redirect, reverse
from django.http import HttpResponse, JsonResponse
from django.views import View
from .models import *


class LoginView(View):
    def get(self, request):
        if ('login') in request.COOKIES:
            li = request.COOKIES.get('login').split(',')
            user = li[0]
            pwd = li[1]
            return render(request, 'login.html', {'user': user, 'pwd': pwd})
        return render(request, 'login.html')

    def post(self, request):
        user = request.POST.get('user')
        pwd = request.POST.get('pwd')
        m = request.POST.get('yy')
        response = HttpResponse()
        users = User.objects.filter(username=user, password=pwd)
        if users:
            if m == '1':
                response = redirect(reverse('allstu'))
                response.set_cookie('login', user + ',' + pwd, max_age=60 * 60 * 24 * 3)
                return response
            else:
                response.delete_cookie('login', path='/login/')
                response = redirect(reverse('allstu'))
        else:
            response.delete_cookie('login', path='/login/')
            return HttpResponse('登录失败')
        return response


class ZhuceView(View):
    def get(self, request):
        return render(request, 'zhuce.html')

    def post(self, request):
        user = request.POST.get('user')
        pwd = request.POST.get('pwd')
        nuser = User(username=user, password=pwd)
        nuser.save()
        return redirect(reverse('login'))


def allstu(request):
    li = request.COOKIES.get('login').split(',')
    user = li[0]
    stu = Student.objects.all()
    return render(request, 'allstu.html', {'stus': stu, 'user': user})


class AddView(View):
    def get(self, request):
        return render(request, 'addstu.html')

    def post(self, request):
        cname = request.POST.get('cname')
        sname = request.POST.get('sname')
        stu = Clazz.objects.filter(cname=cname).first()
        if stu:
            adds = Student(sname=sname, clazz_id=stu.id)
            adds.save()
            return redirect(reverse('allstu'))
        else:
            addc = Clazz(cname=cname)
            addc.save()
            adds = Student(sname=sname, clazz_id=addc.id)
            adds.save()
            return redirect(reverse('allstu'))


def delete(request, id):
    cla = Student.objects.get(id=id)
    cla.delete()
    return redirect(reverse('allstu'))


def deleteall(request):
    list = request.POST.getlist("val")
    if request.method == 'POST':
        print('_______')
        all = request.POST.getlist('val')
        print(all)
        for i in all:
            if i != '':
                a = Student.objects.get(id=i)
                a.delete()
        return redirect(reverse('allstu'))
    return redirect(reverse('allstu'))


def update(request, sid):
    if request.method == 'GET':
        stu = Student.objects.filter(pk=sid)
        return render(request, 'update.html', {'stus': stu})
    else:
        cname = request.POST.get('cname')
        sname = request.POST.get('sname')
        stu = Student.objects.filter(pk=sid).first()
        if cname == "":
            pass
        else:
            cla = Clazz.objects.get(cname=cname)
            if cla:
                stu.clazz_id = cla.id
            else:
                g = Clazz(cname=cname)
                g.save()
                stu.clazz_id = g.id
        if sname == "":
            pass
        else:
            stu.sname = sname
        stu.save()
        return redirect(reverse('allstu'))


def only(request):
    user = request.GET.get('user')
    users = User.objects.filter(username=user).first()
    if users:
        return JsonResponse({'flag': 1})
    else:
        return JsonResponse({'flag': 0})
